const puppeteer = require("puppeteer");
const fs = require("fs-extra");
const path = require("path");
const assert = require("assert");
const progressBar = require("./progressbar");

const log = console.log.bind();
const TOTAL_PAGE = process.argv[2] ? process.argv[2] : 1;
const BASE_DIR = path.resolve(__dirname, "./upload");

// 进度条
const pd = new progressBar("下载进度", 100);
function downLoading(curr, total) {
  if (curr < total) {
    pd.render({ completed: curr, total, status: "下载中..." });
  } else {
    pd.render({ completed: curr, total, status: "下载结束" });
  }
}

// 获取source资源列表
async function getResourceTree(page) {
  var resource = await page._client.send("Page.getResourceTree");
  return resource.frameTree.resources;
}
// 获取source具体资源
async function getImageContent(page, url) {
  const { content, base64Encoded } = await page._client.send(
    "Page.getResourceContent",
    { frameId: String(page.mainFrame()._id), url }
  );
  assert.equal(base64Encoded, true);
  return content;
}

async function getData(page) {
  let pageData = [];
  for (let i = 1; i <= TOTAL_PAGE; i++) {
    page.goto(`https://www.mzitu.com/page/${i}`);
    await page.waitForNavigation();
    const lists = await page.evaluate(async () => {
      let data = [];
      let lis = document.querySelectorAll("#pins li:not(.box)");
      for (let item of lis) {
        let tagA = item.querySelector("span a");
        let { href, innerText } = tagA;
        data.push({
          tip: innerText,
          url: href,
          dir: href.match(/[^\/][\d]+/)[0]
        });
      }
      return data;
    });
    pageData = [...pageData, ...lists];
    await page.waitFor(2500);
  }
  return pageData;
}

async function downPage(page, data, dir) {
  page.goto(`https://www.mzitu.com/${data.dir}/1`);
  await page.waitForNavigation();
  let maxPageNumber = await page.evaluate(() => {
    let tagaArr = document.querySelectorAll(".pagenavi a");
    let Max = tagaArr[tagaArr.length - 2].querySelector("span").innerText || 0;
    return parseInt(Max);
  });
  for (let i = 1; i <= maxPageNumber; i++) {
    downLoading(i, maxPageNumber);
    page.goto(`https://www.mzitu.com/${data.dir}/${i}`);
    await page.waitForNavigation();
    maxPageNumber = await page.evaluate(() => {
      let tagaArr = document.querySelectorAll(".pagenavi a");
      let Max =
        tagaArr[tagaArr.length - 2].querySelector("span").innerText || 0;
      return parseInt(Max);
    });
    let url = await page.$eval(".main-image img", img => img.src);
    let img = `${dir}/${i}${path.extname(url)}`;
    let content = await getImageContent(page, url);
    let buf = Buffer.from(content, "base64");
    fs.writeFileSync(img, buf, "base64");
    await page.waitFor(1000);
  }
  return true;
}

async function downAll(page, pageData) {
  for (const item of pageData) {
    let dir = `${BASE_DIR}/${item.dir}`;
    try {
      fs.accessSync(`${dir}/${item.tip}.txt`);
      continue;
    } catch(err) {}
    console.log(`\ndir:${dir}开始下载`);
    fs.ensureDirSync(dir);
    fs.outputFileSync(`${dir}/${item.tip}.txt`, item.tip);
    await downPage(page, item, dir);
  }
  return true;
}

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    executablePath: "./chrome/chrome.exe",
    defaultViewport: {
      width: 1920,
      height: 1080
    }
  });
  const page = await browser.newPage();
  await page.goto("https://www.mzitu.com/");
  let pageData = await getData(page);
  log(`开始下载总共${pageData.length}页数据`);
  await downAll(page, pageData);
  await browser.close();
  log('下载结束');
  process.exit(0);
})();
